home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Technology Seed / ADC Seed CD - July 1999.toast / Carbon SDK 1.0d10c3 / Sample Code / AppearanceSample / MenuDrawing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-01  |  9.4 KB  |  328 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        MenuDrawing.c
  3.  
  4.     Contains:    Code to demonstrate menu drawing routines of the Appearance Manager.
  5.  
  6.     Version:    Appearance 1.0 SDK
  7.  
  8.     Copyright:    © 1997, 1999 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Edward Voas
  13.  
  14.         Other Contact:        7 of 9, Borg Collective
  15.  
  16.         Technology:            OS Technologies Group
  17.  
  18.     Writers:
  19.  
  20.         (jss)    Jeff Shulman
  21.         (edv)    Ed Voas
  22.  
  23.     Change History (most recent first):
  24.     $Log: MenuDrawing.c,v $
  25.     Revision 1.5  1999/04/15 00:39:24  mattack
  26.     guyf edited the sources so they'd build on 8 and X while menu stuff was
  27.     in flux.
  28.     
  29.  
  30.        <1.4>      4/7/99    jss        Fix CarbonLib compilation problems
  31.          <3>     10/1/97    edv        Update to API changes for drawing menu titles and items.
  32.          <2>     9/11/97    edv        Fix DrawMenuTitle.
  33.          <1>     9/11/97    edv        First checked in.
  34. */
  35.  
  36. #include "AppearanceSamplePrefix.h"
  37.  
  38. #include <MacWindows.h>
  39. #include <Appearance.h>
  40.  
  41. void    DrawMenuStuff();
  42. static pascal void    DrawMenuTitle( const Rect* bounds, SInt16 depth, Boolean colorDevice, long userData );
  43. static void    DrawPhonyMenu();
  44.  
  45. static WindowPtr     gMenuWindow = nil;
  46. static Str255        gMenuItems[] =
  47.                     {
  48.                         "\pFirst Item",
  49.                         "\pSecond Item",
  50.                         "\pThird Item",
  51.                         "\p-",
  52.                         "\pFourth Item",
  53.                         "\pFifth Item"
  54.                     };
  55. static ThemeMenuItemType    gMenuItemTypes[] =
  56.                     {
  57.                         kThemeMenuItemScrollUpArrow,
  58.                         kThemeMenuItemHierarchical,
  59.                         kThemeMenuItemPlain,
  60.                         kThemeMenuItemPlain,
  61.                         kThemeMenuItemPlain,
  62.                         kThemeMenuItemScrollDownArrow
  63.                     };
  64.  
  65. //ããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  66. //    Ä DrawMenuStuff
  67. //ããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  68. //    Draws the menu bar, menu background, etc. for the current theme.
  69. //
  70. void
  71. DrawMenuStuff()
  72. {
  73.     Rect                    bounds, menuBarRect;
  74.     UInt32                    junk;
  75.     Rect                    tempRect, portRect;
  76.     SInt16                    height, widthExtra;
  77.     OSErr                    err;
  78.     CGrafPtr                port;
  79.     MenuTitleDrawingUPP        menuTitleDrawUPP;
  80.  
  81.     if ( gMenuWindow == nil )
  82.     {
  83.         gMenuWindow = GetNewCWindow( 200, nil, (WindowPtr)-1L );
  84.         if ( gMenuWindow == nil ) return;
  85.     }
  86.  
  87.     port = GetWindowPort( gMenuWindow );
  88.     SetPort( port );
  89.     GetPortBounds( port, &portRect );
  90.     EraseRect( &portRect );
  91.     
  92.     TextFont( 0 );
  93.     TextSize( 0 );
  94.     
  95.     bounds = portRect;
  96.     InsetRect( &bounds, 60, 60 );
  97.  
  98.     GetThemeMenuBarHeight( &height );
  99.  
  100.     menuBarRect = bounds;
  101.     menuBarRect.bottom = menuBarRect.top + height;
  102.     
  103.         // Draw a blank normal menu bar
  104.         
  105.     DrawThemeMenuBarBackground( &menuBarRect, kThemeMenuBarNormal, 0 );
  106.     Delay( 60, &junk );
  107.  
  108.         // Draw a blank hilited menu bar
  109.         
  110.     DrawThemeMenuBarBackground( &menuBarRect, kThemeMenuBarSelected, 0 );
  111.     Delay( 60, &junk );
  112.  
  113.         // Draw a menu bar with one menu title
  114.         
  115.     DrawThemeMenuBarBackground( &menuBarRect, kThemeMenuBarNormal, 0 );
  116.     
  117.     menuTitleDrawUPP = NewMenuTitleDrawingProc( DrawMenuTitle );
  118.     
  119.     GetThemeMenuTitleExtra( &widthExtra, false );
  120.     tempRect = menuBarRect;
  121.     tempRect.left += 10;
  122.     tempRect.right = tempRect.left + widthExtra + StringWidth( "\pFile" );
  123.     DrawThemeMenuTitle( &menuBarRect, &tempRect, (UInt16) kThemeMenuActive, 0, menuTitleDrawUPP, (long)"\pFile" );
  124.     Delay( 60, &junk );
  125.  
  126.         // Draw a menu bar with one selected title
  127.         
  128.     DrawThemeMenuBarBackground( &menuBarRect, kThemeMenuBarNormal, 0 );
  129.     tempRect = menuBarRect;
  130.     tempRect.left += 10;
  131.     tempRect.right = tempRect.left + widthExtra + StringWidth( "\pFile" );
  132.     err = DrawThemeMenuTitle( &menuBarRect, &tempRect, (UInt16) kThemeMenuSelected, 0, menuTitleDrawUPP, (long)"\pFile" );
  133.     Delay( 60, &junk );
  134.  
  135.         // Draw a menu bar with one disabled title
  136.         
  137.     DrawThemeMenuBarBackground( &menuBarRect, kThemeMenuBarNormal, 0 );
  138.     tempRect = menuBarRect;
  139.     tempRect.left += 10;
  140.     tempRect.right = tempRect.left + widthExtra + StringWidth( "\pFile" );
  141.     err = DrawThemeMenuTitle( &menuBarRect, &tempRect, (UInt16) kThemeMenuDisabled, 0, menuTitleDrawUPP, (long)"\pFile" );
  142.     Delay( 60, &junk );
  143.  
  144.     EraseRect( &portRect );
  145.     tempRect = bounds;
  146.     DrawThemeMenuBackground( &tempRect, kThemeMenuTypePullDown );
  147.     Delay( 60, &junk );
  148.  
  149.     EraseRect( &portRect );
  150.     tempRect = bounds;
  151.     DrawThemeMenuBackground( &tempRect, kThemeMenuTypePopUp );
  152.     Delay( 60, &junk );
  153.  
  154.     EraseRect( &portRect );
  155.     tempRect = bounds;
  156.     DrawThemeMenuSeparator( &tempRect );
  157.     
  158.  
  159.     SInt16 theHeight, theWidth;
  160.     GetThemeMenuSeparatorHeight( &theHeight );    
  161.     GetThemeMenuItemExtra( kThemeMenuItemPlain, &theHeight, &theWidth );
  162.  
  163.     Delay( 60, &junk );
  164.  
  165.     Rect    temp2;
  166.     
  167.     EraseRect( &portRect );
  168.     tempRect = bounds;
  169.     temp2 = tempRect;
  170. //    InsetRect( &temp2, 0, 20 );
  171.     DrawThemeMenuItem( &temp2, &tempRect, temp2.top, temp2.bottom, kThemeMenuActive, kThemeMenuItemPlain, nil, 0 );
  172.     Delay( 60, &junk );
  173.     DrawThemeMenuItem( &temp2, &tempRect, temp2.top, temp2.bottom, kThemeMenuSelected, kThemeMenuItemPlain, nil, 0 );
  174.     Delay( 60, &junk );
  175.     
  176.     EraseRect( &portRect );
  177.     DrawPhonyMenu();
  178.     
  179.     DisposeMenuTitleDrawingUPP( menuTitleDrawUPP );
  180. }
  181.  
  182. //ããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  183. //    Ä DrawMenuTitle
  184. //ããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  185. //    Draws the menu title centered in the rectangle passed.
  186. //
  187. static pascal void
  188. DrawMenuTitle( const Rect* bounds, SInt16 depth, Boolean colorDevice, long userData )
  189. {
  190.     #pragma unused( depth, colorDevice )
  191.     
  192.     short            height, space, h, v;
  193.     short            charHeight;
  194.     FontInfo        fontInfo;
  195.     StringPtr        string = (StringPtr)userData;
  196.     
  197.     height = bounds->bottom - bounds->top;
  198.     space = bounds->right - bounds->left;
  199.     
  200.     GetFontInfo( &fontInfo );
  201.     
  202.     h = bounds->left;
  203.     charHeight = fontInfo.ascent + fontInfo.descent;
  204.     v = ( height - charHeight ) / 2 + bounds->top + fontInfo.ascent;
  205.     MoveTo( h, v );
  206.  
  207.     DrawString( string );
  208. }
  209.  
  210. //ããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  211. //    Ä DrawPhonyMenu
  212. //ããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  213. //    Draws a menu complete with items. It uses the Appearance Manager routines to calculate
  214. //    the item width and height and also iterates over each item, hiliting and unhiliting it.
  215. //
  216. static void
  217. DrawPhonyMenu()
  218. {
  219.     Rect                    menuRect, itemRect;
  220.     SInt16                    height, width, temp;
  221.     SInt16                    i, itemHeight;
  222.     SInt16                    extraHeight, extraWidth, sepHeight, maxExtraWidth;
  223.     FontInfo                fontInfo;
  224.     UInt32                    junk;
  225.     MenuItemDrawingUPP        menuItemDrawUPP;
  226.     
  227.         // Calculate the width and height of our menu.
  228.     
  229.     GetFontInfo( &fontInfo );
  230.     GetThemeMenuSeparatorHeight( &sepHeight );
  231.     
  232.     height = 0;
  233.     width = 0;
  234.     maxExtraWidth = 0;
  235.     
  236.     itemHeight = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
  237.     
  238.     for ( i = 1; i <= 6; i++ )
  239.     {
  240.         if ( gMenuItems[ i - 1 ][1] != '-' )
  241.         {
  242.                 // Depending on the type of menu item background you're going
  243.                 // to draw, the width may differ. We must make sure we've use the biggest
  244.                 // extraWidth we get, so call GetThemeMenuItemExtra each time thru.
  245.                 
  246.             GetThemeMenuItemExtra( gMenuItemTypes[ i-1 ],  &extraHeight, &extraWidth );
  247.  
  248.             temp = StringWidth( gMenuItems[ i - 1 ] );
  249.             if ( temp > width )
  250.                 width = temp;
  251.             
  252.             if ( extraWidth > maxExtraWidth )
  253.                 maxExtraWidth = extraWidth;
  254.             
  255.             height += (itemHeight + extraHeight);
  256.         }
  257.         else
  258.             height += sepHeight;
  259.     }
  260.     
  261.     width += maxExtraWidth;
  262.     
  263.         // OK, now that we've gotten our height and width, lets draw the menu
  264.         // background for fun.
  265.         
  266.     menuRect.top = 10;
  267.     menuRect.left = 10;
  268.     menuRect.bottom = menuRect.top + height;
  269.     menuRect.right = menuRect.left + width;
  270.     
  271.     DrawThemeMenuBackground( &menuRect, kThemeMenuTypePullDown );
  272.     
  273.         // Now let's draw each item, using the calculations made above. Here
  274.         // we assume that the itemHeights are all the same, but we could have
  275.         // stored the heights and used each individual item's height.
  276.         
  277.     itemRect = menuRect;
  278.     itemRect.bottom = itemRect.top;
  279.     
  280.     menuItemDrawUPP = NewMenuItemDrawingProc( DrawMenuTitle );
  281.  
  282.     for ( i = 1; i <= 6; i++ )
  283.     {
  284.         if ( gMenuItems[ i - 1 ][1] != '-' )
  285.         {
  286.             itemRect.bottom = itemRect.top + itemHeight + extraHeight;
  287.             DrawThemeMenuItem( &menuRect, &itemRect, menuRect.top, menuRect.bottom, (UInt16) kThemeMenuActive, gMenuItemTypes[ i-1 ], menuItemDrawUPP,  (UInt32) &gMenuItems[ i - 1] );
  288.         }
  289.         else
  290.         {
  291.             itemRect.bottom = itemRect.top + sepHeight;
  292.             DrawThemeMenuSeparator( &itemRect );
  293.         }
  294.         itemRect.top = itemRect.bottom;
  295.     }
  296.     Delay( 60, &junk );
  297.     
  298.         // Swell, now let's step thru each item, drawing it in the selected, active,
  299.         // disabled, and then in the active state again.
  300.         
  301.     itemRect = menuRect;
  302.     itemRect.bottom = itemRect.top;
  303.     
  304.     for ( i = 1; i <= 6; i++ )
  305.     {
  306.         if ( gMenuItems[ i - 1 ][1] != '-' )
  307.         {
  308.             itemRect.bottom = itemRect.top + itemHeight + extraHeight;
  309.             DrawThemeMenuItem( &menuRect, &itemRect, menuRect.top, menuRect.bottom, (UInt16) kThemeMenuSelected, gMenuItemTypes[ i-1 ], menuItemDrawUPP,  (UInt32)&gMenuItems[ i - 1] );
  310.             Delay( 30, &junk );
  311.             DrawThemeMenuItem( &menuRect, &itemRect, menuRect.top, menuRect.bottom, (UInt16) kThemeMenuActive, gMenuItemTypes[ i-1 ], menuItemDrawUPP,  (UInt32)&gMenuItems[ i - 1] );
  312.             Delay( 30, &junk );
  313.             DrawThemeMenuItem( &menuRect, &itemRect, menuRect.top, menuRect.bottom, (UInt16) kThemeMenuDisabled, gMenuItemTypes[ i-1 ], menuItemDrawUPP,  (UInt32)&gMenuItems[ i - 1] );
  314.             Delay( 30, &junk );
  315.             DrawThemeMenuItem( &menuRect, &itemRect, menuRect.top, menuRect.bottom, (UInt16) kThemeMenuActive, gMenuItemTypes[ i-1 ], menuItemDrawUPP,  (UInt32)&gMenuItems[ i - 1] );
  316.             Delay( 30, &junk );
  317.         }
  318.         else
  319.         {
  320.             itemRect.bottom = itemRect.top + sepHeight;
  321.             DrawThemeMenuSeparator( &itemRect );
  322.         }
  323.         itemRect.top = itemRect.bottom;
  324.     }
  325.     
  326.     DisposeMenuItemDrawingUPP( menuItemDrawUPP );
  327. }
  328.